package com.dji.compose_learning.ui.view

import android.util.Log
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.*
import androidx.compose.foundation.layout.*
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.positionChange
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlin.math.roundToInt

@Composable
fun DraggablePage() {
    var offsetX by remember {
        mutableStateOf(0f)
    }

    var dragX by remember {
        mutableStateOf(0f)
    }
    var dragY by remember {
        mutableStateOf(0f)
    }

    var tapLocation by remember {
        mutableStateOf(Offset(0f, 0f))
    }

    Surface(modifier = Modifier.fillMaxSize()) {
        Column {
            Text(text = "Drag Me",
                modifier = Modifier
                    .draggable(orientation = Orientation.Horizontal,
                        state = rememberDraggableState(
                            onDelta = {
                                offsetX += it
                            }))
                    .offset { IntOffset(offsetX.roundToInt(), 0) })

            Box(modifier = Modifier
                .size(100.dp)
                .offset {
                    IntOffset(dragX.roundToInt(),
                        dragY.roundToInt())
                }  //这里offset写到pointerInput下面就不管用了！
                .background(MaterialTheme.colorScheme.primary)
                .pointerInput(Unit) {
                    //api提供的drag
//                    detectDragGestures { change, dragAmount ->
//                        dragX += dragAmount.x
//                        dragY += dragAmount.y
//                    }
                    //自定义drag
                    while (true) {
                        val pointId = awaitPointerEventScope {
                            val location = awaitFirstDown()
                            tapLocation = location.position
                            Log.i("buobao-location",
                                "x:${location.position.x},y:${location.position.y}")
                            location.id
                        }

                        awaitPointerEventScope {
                            drag(pointId) { changed ->
                                dragX += (changed.position.x - tapLocation.x)
                                dragY += (changed.position.y - tapLocation.y)
                                Log.i("buobao-drag",
                                    "x:${dragX},y:${dragY}")
                            }
                        }
                    }
                }
//                .offset { IntOffset(dragX.roundToInt(), dragY.roundToInt()) }
            )

            Spacer(modifier = Modifier.height(20.dp))
            SwipeableSample()
            Spacer(modifier = Modifier.height(20.dp))
            SwipeableSample2()
        }
    }
}


@Composable
fun SwipeableSample() {
    val width = 96.dp
    val size = 48.dp
    var state by remember {
        mutableStateOf(true)
    }
    val animate by animateDpAsState(targetValue = if (state) 0.dp else 48.dp,
        animationSpec = tween(durationMillis = 200))

    val colorAnimate1 by animateColorAsState(targetValue = if (state) Color.DarkGray else Color.LightGray)
    val colorAnimate2 by animateColorAsState(targetValue =
    if (state) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.primary.copy(alpha = 0.3f))

    Box(modifier = Modifier
        .width(width)
        .background(colorAnimate1)) {
        Box(modifier = Modifier
            .offset { IntOffset(animate.roundToPx(), 0) } //这里offset必须放在最上面才有效
            .size(size)
            .background(colorAnimate2)
//            .offset { IntOffset(animate.roundToPx(), 0) }
            .clickable {
                state = !state
            })
    }
}

@Composable
fun SwipeableSample2() {
    val width = 96.dp
    val size = 48.dp

//    var dragX by remember {
//        mutableStateOf(0f)
//    }

    val dragXAnimateable = remember {
        Animatable(0f)
    }
    dragXAnimateable.updateBounds(lowerBound = 0f, upperBound = 96.dp.value)
    Box(modifier = Modifier
        .width(width)
        .pointerInput(Unit) {
            while (true) {
                coroutineScope {
                    val pointId = awaitPointerEventScope {
                        awaitFirstDown().id
                    }
                    awaitPointerEventScope {
                        horizontalDrag(pointId) { changed->
                            val dragDistance = dragXAnimateable.value + changed.positionChange().x
                            launch {
                                dragXAnimateable.snapTo(dragDistance)
                            }
                        }
                    }

                    if (dragXAnimateable.value < 48.dp.value) {
                         dragXAnimateable.animateTo(0f)
                    }

                    if (dragXAnimateable.value >= 48.dp.value) {
                        dragXAnimateable.animateTo(96.dp.value)
                    }
                }
            }


//            while (true) {
//            coroutineScope {
//                detectHorizontalDragGestures { change, dragAmount ->
//                    var dragDistance = change.positionChange().x + dragAmount
//                    launch {
//                        dragXAnimateable.animateTo(dragDistance)
//                    }
//                }

//            }
//            }

        }
        .background(Color.DarkGray)) {
        Box(modifier = Modifier
            .offset {
                IntOffset(dragXAnimateable.value.roundToInt(), 0)
            }
            .size(size)
            .background(MaterialTheme.colorScheme.primary))
    }
}


















